package com.control;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clases.base.Boleto;
import com.jdbc.BDManagement;


public class MostrarBol extends HttpServlet {
	private BDManagement bdm = new BDManagement();
       

    public MostrarBol() {
        super();
    }
    
    protected void cargarBoletos(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
    	//String[] string = new String[8];
    	HttpSession session = request.getSession(true);
    	LinkedList<Boleto> listaBoletos = null;
    	listaBoletos = new LinkedList<Boleto>();;
    	ResultSet r = bdm.getAllBoletos();
		while(r.next()){
			/*string[0] = r.getInt(2)+"";
			string[1] = r.getString(3);
			string[2] = r.getDate(4)+"";
			string[3] = r.getString(5);
			string[4] = r.getString(6);
			string[5] = r.getString(7);
			string[6] = r.getString(8);
			string[7] = r.getString(9);*/
			Boleto user = new Boleto(r.getInt(2), r.getString(3), r.getDate(4), r.getInt(5), r.getString(6), r.getString(7), r.getString(8), r.getString(9));
			listaBoletos.add(user);
		}
		//request.setAttribute("listaBoletos", listaBoletos);
    	session.setAttribute("listaBoletos", listaBoletos);
    	this.getServletContext().getRequestDispatcher("/mostrarBoletos.jsp").forward(request, response);
		//response.sendRedirect("/mostrarBoletos.jsp");
	}
    
    
    
    /*
		String accion  = request.getServletPath();//obtiene la ultima parte de la url 
    	if(accion.equals("mostrarBoletos")){
    		List<Boleto> boletos = null;
			try {
				boletos = service.getAllBoletos();
			} catch (SQLException e) {
				e.printStackTrace();
			}
    		request.setAttribute("boletos",boletos);
    		
    		this.getServletContext().getRequestDispatcher("/mostrarBoletos.jsp").forward(request, response);   		
    	}
 */
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			cargarBoletos(request, response);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			cargarBoletos(request, response);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
